*Nov 17 version
*Get rid of missing wages (self-employed) 
*Update all the way to 2016 
*5 segments instead of 10
*Quadratic provincial trends
*Prov*bin and year*bin as in triple diff
*full prov*dummies in lower segment
*Add dummy for integer dollar value
*allow for different effects just after increase



/*******************************
This program is the same as James' code 
except here I use 3 month dummy variable.

That is, dummy for upcoming minimum wage change.


*******************************/










* This version of the code estimates full models for men and women with and without
* the "B" restrictions.log using $logdir\Townsend_hzd_sep_5_2014,text replace

clear
discard
cap log close

set more off
set matsize 800
*set mem 1200m

/* "pub" is a flag for testing the code using the public use files and should be always set to zero when using the data in the RDC. */
local pub=0



* workdata is the directory that contains the working data sets
global workdatadir "P:\Townsend_5609_wages\November2019\WorkingData"
global logdir "P:\Townsend_5609_wages\November_2023\logfiles"
global resultdir "P:\Townsend_5609_wages\November_2023\results"

*global workdatadir "P:\Townsend_3234\data"
*global logdir "P:\Townsend_3234\STATA_code\log_files"
*global resultdir "P:\Townsend_3234\results"

log using $logdir\hzd_estimator_November_6_2023_leavers_main_male.log, replace




/****************************************************
* This is the old data version of min wage changes


/******TL: CREATE DATA SET KEEPING TRACK OF MW CHANGES******/
u $workdatadir\min_wag_1996_2017
sort prov year month

by prov: gen mw=mwage[_n-3]
gen chg=((mwage-mw)/mw)
gen mwchg=chg>0 /*could play with this*/




by prov: gen l5mw=mwage[_n-12]
gen chg=((mwage-l5mw)/l5mw)
gen mwchg=chg>0 /*could play with this*/



keep if year>=1997 & year<=2016
/*****JT: Moved to monthly************/
keep prov year month mwchg
sort prov year month
save $workdatadir\mwchg, replace
clear
/*END OF MW CHG LOOP*/

****************************************************/

/*****************************************************
Create dummy for upcoming min wage change for leavers
****************************************************/

u $workdatadir\min_wag_1996_2017 ,clear

* Get dummy for min wage change coming in next 3 months

bys prov (year month): g time=_n
tsset prov time,delta(1)

g change=mwage-L.mwage
replace change=1 if change!=0 & change!=.
g temp=change

g mwchg=1 if F.temp==1 
replace mwchg=1 if F2.temp==1 
*replace mwchg=1 if F3.temp==1 
replace mwchg=0 if mwchg!=1
replace mwchg=. if time==1
drop temp

keep if year>=1997 & year<=2016
keep prov year month mwchg
save $workdatadir\mwchg_current.dta ,replace



/*****************************************************
Get new minimum wage in third month
****************************************************/

use $workdatadir\min_wag_1996_2017,clear

keep prov year month cpi mwage
bys prov (year month): g time=_n
tsset prov time,delta(1)

g mwage3=F2.mwage
g cpi3=F2.cpi

keep if year>=1997 & year<=2016
keep prov year month mwage3 cpi3

save $workdatadir\min_wag_1996_2017_month3.dta,replace







/*Estimation loops begin here. */

local sex=1


while `sex'<=1 {

	* `k' is the sample counter for broad criteria (see below)

	local k=1
	/*k indicates the data set to be created
	Currently:  1 = full data set
				2 = only those paid by the hour.
				3 = excluding those earning tips. */






	* `j' is the subsample counter (see below)


foreach j of numlist 2 3 {

		* Loads base data set - additional restrictions to follow.
		use $workdatadir\panel_6M_sample,clear

		
		
		/* Drop observations with partial imputation, etc. */

		drop if flag_restrictionsB==1
		*Testing restrictions

		display `data after flag_restrictionsB applied'
		sum

		/* Gender selection */
		if `sex'==1 {
			keep if female==0
		}

		if `sex'==2 {
			keep if female==1
		}

		/*Additional possible deletion criteria */

		if `k'==2 {
			/*Normally paid by hour*/
			keep if hourlywork1==1
		}

		if `k'==3 {
			/*No tips */
			keep if tips1~=1
		}

		/*Subsample criteria based on `j' counter */

		/*j=1 is full sample */
		
		if `j'==2 {
			/* Leavers*/
			drop if lfsstat3==. & tenure3==.
			keep if lfsstat3>2|tenure3<=2	
		}

		if `j'==3 {
			/* Stayers */
			keep if (lfsstat3 <= 2 & tenure3 >= 3)
		}

		if `j'==4 {
			/* Stayers with <=1 year of tenure at start of current quarter */
			keep if (lfsstat3 <= 2 & tenure3 >= 3 & tenure1<=12)
		}
		
		if `j'==5 {
			/* stayers with >1 -5  year2 of tenure at start of current quarter */
			keep if (lfsstat3 <= 2 & tenure3 >= 3 & tenure1>12 & tenure1<=60)
		}
		
		if `j'==6 {
			/* stayers with >5 years of tenure at start of current quarter */
			keep if (lfsstat3 <= 2 & tenure3 >= 3 & tenure1>=61)
		}

		display `sex' `j' `k'

		display `data after additional restrictions applied'
		sum

		*sum female lfsstat1 lfsstat2 hourlywork1 tips1


		drop if wage1<.2

		*sum age female educlev prov wage1 tenure1 [aw=finalwt1]

		*Sample restrictions beyond basicsdrop if flag_restrictionsB==1

		rename smth1 month
		rename syear1 year

		sort prov year month


		/*merge prov year month using P:\Townsend_3234\data\min_wag_2013*/
		/*****TL: NEW MIN WAGE DATA SET FOR 2016*****/
		merge m:1 prov year month using $workdatadir\min_wag_1996_2017
		drop if _merge==2
		drop _merge

		merge m:1 prov year month using $workdatadir\min_wag_1996_2017_month3
		drop if _merge==2
		drop _merge

		gen rdvwag=wage1/cpi*100
		/*****TL: DEFLATE THE MINIMUM WAGE, WAS ALREADY DONE FOR 1997-2011*/	
		gen rminw=mwage/cpi*100  
		gen rminw3=mwage3/cpi3*100  

		gen agecat=.
		replace agecat=1 if age>=15&age<=19
		replace agecat=2 if age>=20&age<=24
		replace agecat=3 if age>=25&age<=34
		replace agecat=4 if age>=35&age<=54
		replace agecat=5 if age>=55&age<=60

		drop if agecat==.



		label define AGECAT 1 "15-19" 2 "20-24" 3 "25-34" 4 "35-54" 5 "55-60"
		label value agecat AGECAT

		gen edgrp=.
		replace edgrp=1 if educlev>=0&educlev<=2
		replace edgrp=2 if educlev==3|educlev==4
		replace edgrp=3 if educlev>=5&educlev<=7
		replace edgrp=4 if educlev==8|educlev==9

		drop if edgrp==.

		table edgrp educlev

		label define EDGRP 1 "Less than H.S." 2 "H.S. graduate and/or some P.S." 3 "P.S. Diploma or Cert." 4 "B.A. or more"
		label value edgrp EDGRP






		************************************************************
		*Code for lagged minimum wage

		*preserve

		*collapse mwage rminw,by(prov year)
		*gen mwage1=mwage[_n-1]
		*gen rminw1=rminw[_n-1]
		 

		*save d:\min_wag\data\min_one_year,replace
		*restore
		*rename _merge old_merge
			
		*merge m:1 prov year using d:\min_wag\data\min_one_year
		************************************************************

		gen unemp=1-(lfsstat1<=2)
		*keep if unemp==0
		sum unemp
		*gen ftstudent=schooln==2|schooln==4|schooln==6|schooln==8

		*drop if ftstudent==1

		*keep if age_12<=3

		*keep if educ90<=2
		*keep if cowmain<=2

		*keep if educlev<=3


		set more off

		gen wagcat=0
		replace wagcat=1 if rdvwag<=3
		replace wagcat=2 if rdvwag>3 & rdvwag<=3.5
		replace wagcat=3 if rdvwag>3.5 & rdvwag<=4



		/****TL: EXPAND LOOP TO FIND IF INTEGER VALUE IN THE WAGE CATEGORY*/

		local i=1
		while `i'<=160{
			replace wagcat=3+`i' if rdvwag>4+(`i'-1)*.1 & rdvwag<=4+`i'*.1
			local i=`i'+1
		}

		replace wagcat=164 if rdvwag>20
		*replace wagcat=145 if unemp==1
		*gen delta=(wagcat==145)
		*min wage & lages assigned here

		/***************JT: Fill in data set for identifying integer. */
		preserve
		sort prov year month wagcat
		collapse cpi,by(prov year month wagcat)

		fillin prov year month wagcat
		sum _fillin

		egen cpimax=max(cpi),by(prov year month)
		replace cpi=cpimax if cpi==.
		gen cut_l=0
		gen cut_h=0
		replace cut_l=0 if wagcat==1
		replace cut_l=3*cpi/100 if wagcat==2
		replace cut_l=3.5*cpi/100 if wagcat==3
		replace cut_h=3*cpi/100 if wagcat==1
		replace cut_h=3.5*cpi/100 if wagcat==2
		replace cut_h=4*cpi/100 if wagcat==3
		local i=1
		while `i'<=160{
			replace cut_l=(4+(`i'-1)*.1)*cpi/100 if wagcat==`i'+3	
			replace cut_h=(4+`i'*.1)*cpi/100 if wagcat==`i'+3
			local i=`i'+1
		}
		drop if cpi==.

		sum

		/****TL: INTEGER VALUE IN THE WAGE CATEGORY*/
		gen dollar=(floor(cut_h)>cut_l)
		summ dollar cut_l cut_h

		/*Could include province in list if cpi was done at the monthly level...*/
		keep if prov==35
		keep year month wagcat dollar
		sum
		sort year month wagcat
		table year month
		save $workdatadir\dollar_bins,replace

		restore





		table wagcat,stat(min rdvwag)  stat(max rdvwag)
		 


		gen mincat=0
		replace mincat=1 if rminw<=3
		replace mincat=2 if rminw>3 & rminw<=3.5
		replace mincat=3 if rminw>3.5 & rminw<=4

		local i=1
		while `i'<=76{
			replace mincat=3+`i' if rminw>4+(`i'-1)*.1 & rminw<=4+`i'*.1
			local i=`i'+1
		}

		*Check for rank of mincat
		sum female mincat
		gen mincat1=0

		***********************************
		* Temporary code alteration...fix!*
		***********************************
		gen rminw1=0 
		replace mincat1=1 if rminw1<=3
		replace mincat1=2 if rminw1>3 & rminw1<=3.5
		replace mincat1=3 if rminw1>3.5 & rminw1<=4

		local i=1
		while `i'<=76{
			replace mincat1=3+`i' if rminw1>4+(`i'-1)*.1 & rminw1<=4+`i'*.1
			local i=`i'+1
		}

		gen plus=mincat>mincat1
		gen minus=mincat<mincat1



		gen mincatn=0
		replace mincatn=1 if rminw3<=3
		replace mincatn=2 if rminw3>3 & rminw3<=3.5
		replace mincatn=3 if rminw3>3.5 & rminw3<=4

		local i=1
		while `i'<=76{
			replace mincatn=3+`i' if rminw3>4+(`i'-1)*.1 & rminw3<=4+`i'*.1
			local i=`i'+1
		}



		*table prov year,c(mean plus mean minus)

		 
		gen one=1


		 
		rename finalwt1 fweight 
		 
		rename agecat agegrp 

		compress 
		 
		sort prov year month female agegrp edgrp wagcat mincat mincatn
		collapse mincat1 (sum) fweight (sum) one,by(prov year month female agegrp edgrp wagcat mincat mincatn) 

		fillin prov year month female agegrp edgrp wagcat 

		*Checks for obs where entire spell is created by fillin command.
		egen filled=sum(_fillin),by(prov year month female agegrp edgrp)
		sum filled
		drop if filled==164
		sum filled

		egen minmax=max(mincat),by(prov year month)
		replace mincat=minmax if mincat==.
		drop minmax

		egen minmax=max(mincatn),by(prov year month)
		replace mincatn=minmax if mincatn==.

		sort year month wagcat
		merge m:1 year month wagcat using $workdatadir\dollar_bins
		drop _merge


		*egen minmax1=max(mincat1),by(prov year month)
		*replace mincat1=minmax1 if mincat1==.

		*Fill in bins with no casualties*
		replace fweight=0 if fweight==.


		gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))



		sum dollar
		summ dollar [w=fweight]

		/*****TL: MERGE TO MW CHG TO CREATE NEW DUMMIES*****/

		/*****IS: NEED TO CHANGE FILE HERE AS REQUIRED*****/
		sort prov year month
		merge m:1 prov year month using $workdatadir\mwchg_current
		drop _merge
		
		//  CONFIRM THIS
		// Don't want duplicate observations for cases where there is no min wage change
		replace mincatn=0 if mwchg==0	

		
		/********JT: removed sub as a criterion....not used below*/

		sort prov year month female agegrp edgrp wagcat mincat mincatn dollar mwchg

		collapse (sum) fweight (sum) one,by(prov year month female agegrp edgrp wagcat mincat mincatn dollar mwchg) 

		compress


		/*Insert estimation code here */

		set more off


		*Fillin command creates observations for early 1996 as well, which have no obs. from the LFS.
		*also observations in 2012 (December) that are empty.
		drop if mincat==. & year==1996
		drop if mincat==. & year==2016
		drop if year==1996 /*currently missing 1995 minimum wages.*/
		*Drop if group has failed in earlier bins.


		*Selection criteria


		*gen month=11
		*gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))
		*drop month

		gen diff=wagcat-mincat
		gen diff2=wagcat-mincatn
		

		gen min6b1=diff<-5
		gen min35b1=diff>=-5&diff<=-3
		gen min12b1=diff==-2|diff==-1
		gen min1=diff==0
		gen min12a1=diff>=1&diff<=2
		gen min35a1=diff>=3&diff<=5
		gen min610a1=diff>=6&diff<=10
		gen min1115a1=diff>=11&diff<=15
		gen min1620a1=diff>=16&diff<=20
		gen min2125a1=diff>=21&diff<=25
		gen min2630a1=diff>=26&diff<=30	
		gen min3135a1=diff>=31&diff<=35
		gen min3640a1=diff>=36&diff<=40	
		gen min4145a1=diff>=41&diff<=45
		gen min4650a1=diff>=46&diff<=50	
		
		
		/****IS: Bins around old and new min wage****/
		gen minold2new=1 if diff>0 & diff2<0
		replace minold2new=0 if minold2new!=1
		gen minnew=diff2==0

		gen min12a2=diff2>=1&diff2<=2
		gen min35a2=diff2>=3&diff2<=5
		gen min610a2=diff2>=6&diff2<=10
		gen min1115a2=diff2>=11&diff2<=15
		gen min1620a2=diff2>=16&diff2<=20
		gen min2125a2=diff2>=21&diff2<=25
		gen min2630a2=diff2>=26&diff2<=30
		gen min3135a2=diff2>=31&diff2<=35
		gen min3640a2=diff2>=36&diff2<=40
		gen min4145a2=diff2>=41&diff2<=45
		gen min4650a2=diff2>=46&diff2<=50



		*gen smin6b=min6b*sub
		*gen smin35b=min35b*sub
		*gen smin12b=min12b*sub
		*gen smin=min*sub
		*gen smin13a=min13a*sub
		*gen smin410a=min410a*sub
		*gen smin1120a=min1120a*sub
		*gen smin2130a=min2130a*sub
		*gen smin3140a=min3140a*sub
		*gen smin4150a=min4150a*sub



		gen cut=wagcat

		/****TL: FIVE SEGMENTS INSTEAD OF TEN****/
			gen part0=.
			  replace part0=1 if cut<=63  /*up to $10*/
			  replace part0=2 if cut>=64&cut<=88 /*$10 to $12.5*/
			  replace part0=3 if cut>=89&cut<=113 /*$12.5 to $15.0*/
			  replace part0=4 if cut>=114&cut<=138 /*$15 to $17.5*/
			  replace part0=5 if cut>=139&cut<=163 /*$17.5 to $20*/
			*replace part0=1 if cut<=33  /*up to $7*/
			*replace part0=2 if cut>=34&cut<=43 /*$7 to $8*/
			*replace part0=3 if cut>=44&cut<=53 /*$8 to $9*/
			*replace part0=4 if cut>=54&cut<=63 /*$9 to $10*/
			*replace part0=5 if cut>=64&cut<=73 /*$10 to $11*/
			*replace part0=6 if cut>=74&cut<=83 /*$11 to $12*/
			*replace part0=7 if cut>=84&cut<=98 /*$12 to $13.5*/
			*replace part0=8 if cut>=99&cut<=113 /*$13.5 to $15*/
			*replace part0=9 if cut>=114&cut<=138 /*$15 to $17.5*/
			*replace part0=10 if cut>=139&cut<=163 /*$17.5 to $20*/
			*replace part0=11 if cut==164 /*$20 and above*/

		gen part=.
			replace part=1 if cut<=36
			replace part=2 if cut>=37&cut<=42
			replace part=3 if cut>=43&cut<=48
			replace part=4 if cut>=49&cut<=55
			replace part=5 if cut>=56&cut<=64
			replace part=6 if cut>=65&cut<=74
			replace part=7 if cut>=75&cut<=88
			replace part=8 if cut>=89&cut<=103
			replace part=9 if cut>=104&cut<=124
			replace part=10 if cut>=125&cut<=163
			replace part=11 if cut==164

			
			
			

		*removed sub...added mwchg
		
		
		
		sort prov year month agegrp edgrp mincat mincatn mwchg wagcat
		bys prov year month agegrp edgrp mincat mincatn mwchg (wagcat): g rightsum=sum(fweight)
		bys prov year month agegrp edgrp mincat mincatn mwchg (wagcat): egen totalsum=max(rightsum)
		g remain=totalsum-rightsum+fweight
		drop rightsum totalsum
		
		// Original code which is too slow
		/*
		g remain2=0
		local i=1
		while `i'<=10 {
		egen rightsum=sum(fweight*(wagcat>=`i')),by(prov year quarter agegrp edgrp mincat mincatn mwchg)
		*gen hzd=fweight/rightsum
		replace remain2=rightsum if cut==`i'
		*replace h1=hzd if cut==`i'
		gen byte bin`i'=cut==`i'
		drop rightsum 
		local i=`i'+1
		}
		*/
		local i=1
		while `i'<=164 {
			gen byte bin`i'=cut==`i'
			local i=`i'+1
		}		
		
		gen quarter=.
		replace quarter=1 if month>=1&month<=3
		replace quarter=2 if month>=4&month<=6
		replace quarter=3 if month>=7&month<=9
		replace quarter=4 if month>=10&month<=12

		*table month quarter
		label define QUARTER 1 "Jan.-Mar." 2 "Apr.-Jun." 3 "Jul.-Sep." 4 "Oct.-Dec."
		label value quarter QUARTER
		
		
		compress

		save $resultdir\data_leavers_`sex'_`k'_`j'_v1,replace


		local i=1
		while `i'<=5 {
		*Variables used in regression
		gen byte p10_`i'=prov==10 & part0 ==`i' 
		gen byte p11_`i'=prov==11 & part0 ==`i' 
		gen byte p12_`i'=prov==12 & part0 ==`i' 
		gen byte p13_`i'=prov==13 & part0 ==`i' 
		gen byte p24_`i'=prov==24 & part0 ==`i' 
		gen byte p46_`i'=prov==46 & part0 ==`i' 
		gen byte p47_`i'=prov==47 & part0 ==`i' 
		gen byte p48_`i'=prov==48 & part0 ==`i' 
		gen byte p59_`i'=prov==59 & part0 ==`i' 

		gen byte a1_`i'=agegrp==1& part0==`i'
		gen byte a2_`i'=agegrp==2& part0==`i'
		gen byte a3_`i'=agegrp==3& part0==`i'
		gen byte a5_`i'=agegrp==5& part0==`i'

		gen byte e1_`i'=edgrp==1& part0==`i'
		gen byte e2_`i'=edgrp==2& part0==`i'
		gen byte e4_`i'=edgrp==4& part0==`i'

		gen byte q1_`i'=quarter==1& part0==`i'
		gen byte q2_`i'=quarter==2& part0==`i'
		gen byte q4_`i'=quarter==4& part0==`i'

		*Commented out 1996 and 2012 for public use files.
		*gen byte y96_`i'=year==1996 & part0 ==`i' 
		gen byte y97_`i'=year==1997 & part0 ==`i' 
		gen byte y98_`i'=year==1998 & part0 ==`i' 
		gen byte y00_`i'=year==2000 & part0 ==`i'
		gen byte y01_`i'=year==2001 & part0 ==`i' 
		gen byte y02_`i'=year==2002 & part0 ==`i' 
		gen byte y03_`i'=year==2003 & part0 ==`i' 
		gen byte y04_`i'=year==2004 & part0 ==`i'
		gen byte y05_`i'=year==2005 & part0 ==`i' 
		gen byte y06_`i'=year==2006 & part0 ==`i' 
		gen byte y07_`i'=year==2007 & part0 ==`i' 
		gen byte y08_`i'=year==2008 & part0 ==`i'
		gen byte y09_`i'=year==2009 & part0 ==`i' 
		gen byte y10_`i'=year==2010 & part0 ==`i' 
		gen byte y11_`i'=year==2011 & part0 ==`i' 
		gen byte y12_`i'=year==2012 & part0 ==`i'
		gen byte y13_`i'=year==2013 & part0 ==`i'
		gen byte y14_`i'=year==2014 & part0 ==`i'
		gen byte y15_`i'=year==2015 & part0 ==`i'
		gen byte y16_`i'=year==2016 & part0 ==`i'

		/*****TL: ADD PROVINCIAL TRENDS****/
		*gen byte ont_`i'=prov==35 & part0 ==`i' 
		gen tr10_`i'=(year-1999)*p10_`i' 
		gen tr11_`i'=(year-1999)*p11_`i' 
		gen tr12_`i'=(year-1999)*p12_`i' 
		gen tr13_`i'=(year-1999)*p13_`i' 
		gen tr24_`i'=(year-1999)*p24_`i' 
		*gen tr35_`i'=(year-1999)*ont_`i' 
		gen tr46_`i'=(year-1999)*p46_`i' 
		gen tr47_`i'=(year-1999)*p47_`i' 
		gen tr48_`i'=(year-1999)*p48_`i' 
		gen tr59_`i'=(year-1999)*p59_`i' 

		gen tq10_`i'=(year-1999)^2*p10_`i' 
		gen tq11_`i'=(year-1999)^2*p11_`i' 
		gen tq12_`i'=(year-1999)^2*p12_`i' 
		gen tq13_`i'=(year-1999)^2*p13_`i' 
		gen tq24_`i'=(year-1999)^2*p24_`i' 
		*gen tq35_`i'=(year-1999)^2*ont_`i' 
		gen tq46_`i'=(year-1999)^2*p46_`i' 
		gen tq47_`i'=(year-1999)^2*p47_`i' 
		gen tq48_`i'=(year-1999)^2*p48_`i' 
		gen tq59_`i'=(year-1999)^2*p59_`i' 

		/******ADD PROV*BIN AND YEAR*BIN CONTROLS*****/
		gen bp10_`i'=(wagcat-(14+25*`i'))*p10_`i' 
		gen bp11_`i'=(wagcat-(14+25*`i'))*p11_`i' 
		gen bp12_`i'=(wagcat-(14+25*`i'))*p12_`i' 
		gen bp13_`i'=(wagcat-(14+25*`i'))*p13_`i' 
		gen bp24_`i'=(wagcat-(14+25*`i'))*p24_`i' 
		gen bp46_`i'=(wagcat-(14+25*`i'))*p46_`i' 
		gen bp47_`i'=(wagcat-(14+25*`i'))*p47_`i' 
		gen bp48_`i'=(wagcat-(14+25*`i'))*p48_`i' 
		gen bp59_`i'=(wagcat-(14+25*`i'))*p59_`i' 

		*gen by96_`i'=(wagcat-(14+25*`i'))*y96_`i'
		gen by97_`i'=(wagcat-(14+25*`i'))*y97_`i' 
		gen by98_`i'=(wagcat-(14+25*`i'))*y98_`i' 
		gen by00_`i'=(wagcat-(14+25*`i'))*y00_`i' 
		gen by01_`i'=(wagcat-(14+25*`i'))*y01_`i' 
		gen by02_`i'=(wagcat-(14+25*`i'))*y02_`i' 
		gen by03_`i'=(wagcat-(14+25*`i'))*y03_`i' 
		gen by04_`i'=(wagcat-(14+25*`i'))*y04_`i' 
		gen by05_`i'=(wagcat-(14+25*`i'))*y05_`i' 
		gen by06_`i'=(wagcat-(14+25*`i'))*y06_`i' 
		gen by07_`i'=(wagcat-(14+25*`i'))*y07_`i' 
		gen by08_`i'=(wagcat-(14+25*`i'))*y08_`i' 
		gen by09_`i'=(wagcat-(14+25*`i'))*y09_`i' 
		gen by10_`i'=(wagcat-(14+25*`i'))*y10_`i' 
		gen by11_`i'=(wagcat-(14+25*`i'))*y11_`i' 
		gen by12_`i'=(wagcat-(14+25*`i'))*y12_`i' 
		gen by13_`i'=(wagcat-(14+25*`i'))*y13_`i' 
		gen by14_`i'=(wagcat-(14+25*`i'))*y14_`i' 
		gen by15_`i'=(wagcat-(14+25*`i'))*y15_`i' 
		gen by16_`i'=(wagcat-(14+25*`i'))*y16_`i' 


		local i=`i'+1
		}

		/*****TL: ADD A FULL SET OF prov*year DUMMIES (SEGMENT 1)***/
		gen provyear=100*prov+(year-1997) if part0==1
		replace provyear=9999 if part0>=2
		quietly tab provyear, gen(py)

		/*NOTE: ONTARIO IN 1999 IS py103*/
		/*Note: without 1996, py200 is the last valid dummy (py201 is for obs in the second and higher partition */

		gen cmin6b=mwchg*min6b1
		gen cmin35b=mwchg*min35b1
		gen cmin12b=mwchg*min12b1
		gen cmin=mwchg*min1
		gen cminold2new=mwchg*minold2new
		gen cminnew=mwchg*minnew
		gen cmin12a=mwchg*min12a2
		gen cmin35a=mwchg*min35a2
		gen cmin610a=mwchg*min610a2
		gen cmin1115a=mwchg*min1115a2
		gen cmin1620a=mwchg*min1620a2
		gen cmin2125a=mwchg*min2125a2
		gen cmin2630a=mwchg*min2630a2
		gen cmin3135a=mwchg*min3135a2
		gen cmin3640a=mwchg*min3640a2
		gen cmin4145a=mwchg*min4145a2
		gen cmin4650a=mwchg*min4650a2


		gen dolm=dollar*min1

		/*glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a cmin cmin6b cmin35b cmin12b cmin12a ///
		cmin35a cmin610a cmin1115a cmin1620a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)


		glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a  ///
		dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\aug24nd_`sex'_`k'_`j',replace

		 glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a min2125a min2630a ///
		 dollar dolm ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\dec15nda_`sex'_`k'_`j',replace 

		glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a  ///
		cmin cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\aug24d_`sex'_`k'_`j',replace

		glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a min2125a min2630a ///
		cmin cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a cmin2125a cmin2630a dollar dolm ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\dec15da_`sex'_`k'_`j',replace */

egen est_nobs=sum(one)
sum est_nobs

		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1 ///
		cmin cminold2new cminnew cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a cmin2125a cmin2630a ///
		cmin3135a cmin3640a cmin4145a cmin4650a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\leavers_main_`sex'_`k'_`j'_v1_5,replace
		
		*Min only up to $4.50
		test min4650a1 cmin4650a
		*Min only up to $4.00
		test min4145a1 min4650a1 cmin4145a cmin4650a
		*Min only up to $3.50
		test min3640a1 min4145a1 min4650a1 cmin3640a cmin4145a cmin4650a
		*Min only up to $3.00
		test min3135a1 min3640a1 min4145a1 min4650a1 cmin3135a cmin3640a cmin4145a cmin4650a
		*Min only up to $2.50
		test min2630a1 min3135a1 min3640a1 min4145a1 min4650a1 cmin2630a cmin3135a cmin3640a cmin4145a cmin4650a
		*Min only up to $2.00.
		test min2125a1 min2630a1 min3135a1 min3640a1 min4145a1 min4650a1 cmin2125a cmin2630a cmin3135a cmin3640a cmin4145a cmin4650a

/*		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 min1115a1 min1620a1   ///
		cmin cminold2new cminnew cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain)

		est save $resultdir\leavers_main_`sex'_`k'_`j'_v2,replace			} */
		
erase $resultdir\data_leavers_`sex'_`k'_`j'_v1.dta

 }  /*End j-loop  */
	 

	*local k=`k'+1
	*}

	local sex=`sex'+1
} 


log close

/*































reg fweight min min6b min35b min12b min12a min35a min610a ///
min1115a min1620a min2125a min2630a ///
cmin cmin6b cmin35b cmin12b cmin12a cmin35a ///
cmin610a cmin1115a cmin1620a cmin2125a cmin2630a dollar ///
 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
 bin* if remain>0 &wagcat~=164

*******************************************************************
* Run from here!
*******************************************************************

local j=1

while `j'<=4 {

if (`j'==1|`j'==2) {

use $workdatadir\panel_fillin_unrestrict,clear
}
if (`j'==3|`j'==4) {
use $workdatadir\panel_fillin_restrict,clear
}

*keep if agegrp==1|agegrp==4
*keep if edgrp==2|edgrp==4
*keep if year==1997|year==2000|year==2005|year==2010
*keep if quarter==1 | quarter==3
*keep if prov==10|prov==35|prov==48|prov==59

if (`j'==1|`j'==3) {
keep if female==0
}

if (`j'==2|`j'==4) {
keep if female==1
}


 
 ***********************
 *Takes results from above and creates fitted provincial densities...
 *
 *
 *
 ***********************
 
local j=1
while `j'<=4 {
 
 
if (`j'==1|`j'==2) {
use $workdatadir\panel_fillin_unrestrict,clear
}
if (`j'==3|`j'==4) {
use $workdatadir\panel_fillin_restrict,clear
}

*keep if agegrp==1|agegrp==4
*keep if edgrp==2|edgrp==4
*keep if year==1997|year==2000|year==2005|year==2010
*keep if quarter==1 | quarter==3
*keep if prov==10|prov==35|prov==48|prov==59

if (`j'==1|`j'==3) {
keep if female==0
}

if (`j'==2|`j'==4) {
keep if female==1
}

set more off
set matsize 500

sum

*Fillin command creates observations for early 1996 as well, which have no obs. from the LFS.
*also observations in 2012 (December) that are empty.
drop if mincat==. & year==1996
drop if mincat==. & year==2012
*Drop if group has failed in earlier bins.





*gen month=11
*gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))
*drop month

gen diff=wagcat-mincat
gen min6b=diff<-5
gen min35b=diff>=-5&diff<=-3
gen min12b=diff==-2|diff==-1
gen min=diff==0
gen min13a=diff>=1&diff<=3
************Important - next line has changed definition of min410a
gen min410a=diff>=4&diff<=10
gen min1120a=diff>=11&diff<=20
gen min2130a=diff>=21&diff<=30
gen min3140a=diff>=31&diff<=40
gen min4150a=diff>=41&diff<=50

gen smin6b=min6b*sub
gen smin35b=min35b*sub
gen smin12b=min12b*sub
gen smin=min*sub
gen smin13a=min13a*sub
gen smin410a=min410a*sub
gen smin1120a=min1120a*sub
gen smin2130a=min2130a*sub
gen smin3140a=min3140a*sub
gen smin4150a=min4150a*sub



gen cut=wagcat

	gen part0=.
	replace part0=1 if cut<=33
	replace part0=2 if cut>=34&cut<=43
	replace part0=3 if cut>=44&cut<=53
	replace part0=4 if cut>=54&cut<=63
	replace part0=5 if cut>=64&cut<=73
	replace part0=6 if cut>=74&cut<=83
	replace part0=7 if cut>=84&cut<=98
	replace part0=8 if cut>=99&cut<=113
	replace part0=9 if cut>=114&cut<=138
	replace part0=10 if cut>=139&cut<=163
	replace part0=11 if cut==164

gen part=.
	replace part=1 if cut<=36
	replace part=2 if cut>=37&cut<=42
	replace part=3 if cut>=43&cut<=48
	replace part=4 if cut>=49&cut<=55
	replace part=5 if cut>=56&cut<=64
	replace part=6 if cut>=65&cut<=74
	replace part=7 if cut>=75&cut<=88
	replace part=8 if cut>=89&cut<=103
	replace part=9 if cut>=104&cut<=124
	replace part=10 if cut>=125&cut<=163
	replace part=11 if cut==164

		
gen remain=0	


sort prov year quarter agegrp edgrp mincat sub



*Rightsumnot needed for fitting values.
local i=1
while `i'<=164 {
*egen rightsum=sum(fweight*(wagcat>=`i')),by(prov year quarter agegrp edgrp mincat sub)
*gen hzd=fweight/rightsum
*replace remain=rightsum if cut==`i'
*replace h1=hzd if cut==`i'
gen byte bin`i'=cut==`i'
*drop rightsum 
local i=`i'+1
}

compress





local i=1
while `i'<=10 {
*Variables used in regression
gen byte p10_`i'=prov==10 & part0 ==`i' 
gen byte p11_`i'=prov==11 & part0 ==`i' 
gen byte p12_`i'=prov==12 & part0 ==`i' 
gen byte p13_`i'=prov==13 & part0 ==`i' 
gen byte p24_`i'=prov==24 & part0 ==`i' 
gen byte p46_`i'=prov==46 & part0 ==`i' 
gen byte p47_`i'=prov==47 & part0 ==`i' 
gen byte p48_`i'=prov==48 & part0 ==`i' 
gen byte p59_`i'=prov==59 & part0 ==`i' 

gen byte a1_`i'=agegrp==1& part0==`i'
gen byte a2_`i'=agegrp==2& part0==`i'
gen byte a3_`i'=agegrp==3& part0==`i'
gen byte a5_`i'=agegrp==5& part0==`i'

gen byte e1_`i'=edgrp==1& part0==`i'
gen byte e2_`i'=edgrp==2& part0==`i'
gen byte e4_`i'=edgrp==4& part0==`i'

gen byte q1_`i'=quarter==1& part0==`i'
gen byte q2_`i'=quarter==2& part0==`i'
gen byte q4_`i'=quarter==4& part0==`i'

gen byte y96_`i'=year==1996 & part0 ==`i' 
gen byte y97_`i'=year==1997 & part0 ==`i' 
gen byte y98_`i'=year==1998 & part0 ==`i' 
gen byte y00_`i'=year==2000 & part0 ==`i'
gen byte y01_`i'=year==2001 & part0 ==`i' 
gen byte y02_`i'=year==2002 & part0 ==`i' 
gen byte y03_`i'=year==2003 & part0 ==`i' 
gen byte y04_`i'=year==2004 & part0 ==`i'
gen byte y05_`i'=year==2005 & part0 ==`i' 
gen byte y06_`i'=year==2006 & part0 ==`i' 
gen byte y07_`i'=year==2007 & part0 ==`i' 
gen byte y08_`i'=year==2008 & part0 ==`i'
gen byte y09_`i'=year==2009 & part0 ==`i' 
gen byte y10_`i'=year==2010 & part0 ==`i' 
gen byte y11_`i'=year==2011 & part0 ==`i' 
gen byte y12_`i'=year==2012 & part0 ==`i'

local i=`i'+1
}
describe
compress

set matsize 800


if (`j'==1|`j'==3) {
est use $resultdir\males_unrestricted_jul_24_14
}
if (`j'==2|`j'==4) {
est use $resultdir\females_unrestricted_jul_24_14
}

est store m1

predict zu, mu
predict xbu, xb

gen hzdu=exp(xbu)

gen hzdminu=exp(_b[min]*min+_b[min6b]*min6b+ _b[min35b]*min35b + _b[min12b]*min12b + _b[min13a]*min13a + _b[min410a]*min410a+_b[min1120a]*min1120a +_b[min2130a]*min2130a+_b[min3140a]*min3140a+_b[min4150a]*min4150a ///
	+_b[smin]*smin+_b[smin6b]*smin6b+ _b[smin35b]*smin35b + _b[smin12b]*smin12b + _b[smin13a]*smin13a + _b[smin410a]*smin410a+_b[smin1120a]*smin1120a +_b[smin2130a]*smin2130a+_b[smin3140a]*smin3140a+_b[smin4150a]*smin4150a)

gen hzdlatu=exp(xbu)/hzdminu

sum hzdu hzdlatu hzdminu

if (`j'==1|`j'==3) {
est use $resultdir\males_restricted_jul_24_14
}
if (`j'==2|`j'==4) {
est use $resultdir\females_restricted_jul_24_14
}
est store m2

estout m1 m2,stats(N)

predict zr, mu
predict xbr, xb

gen hzdr=exp(xbr)

gen hzdminr=exp(_b[min]*min+_b[min6b]*min6b+ _b[min35b]*min35b + _b[min12b]*min12b + _b[min13a]*min13a + _b[min410a]*min410a+_b[min1120a]*min1120a +_b[min2130a]*min2130a+_b[min3140a]*min3140a+_b[min4150a]*min4150a ///
	+_b[smin]*smin+_b[smin6b]*smin6b+ _b[smin35b]*smin35b + _b[smin12b]*smin12b + _b[smin13a]*smin13a + _b[smin410a]*smin410a+_b[smin1120a]*smin1120a +_b[smin2130a]*smin2130a+_b[smin3140a]*smin3140a+_b[smin4150a]*smin4150a)

gen hzdlatr=exp(xbr)/hzdminr
sum hzdr hzdlatr hzdminr




gen hu=zu/remain
gen hr=zr/remain
  
 
gen double x_sect=year*1000000000+prov*10000000+mincat*100000+quarter*1000+agegrp*100+edgrp*10+sub
xtset x_sect wagcat


 
gen avghzdlatu=1/3*L1.hzdlatu+1/3*hzdlatu+1/3*F1.hzdlatu
replace avghzdlatu=3/5*hzdlatu+1/5*F1.hzdlatu+1/5*F2.hzdlatu if wagcat==1
replace avghzdlatu=1/5*L1.hzdlatu +2/5*hzdlatu+1/5*F1.hzdlatu + 1/5*F2.hzdlatu if wagcat==2
replace avghzdlatu=3/5*hzdlatu+1/5*L1.hzdlatu+1/5*L2.hzdlatu if wagcat==163
replace avghzdlatu=1/5*F1.hzdlatu +2/5*hzdlatu+1/5*L1.hzdlatu + 1/5*L2.hzdlatu if wagcat==162
replace avghzdlatu=1 if wagcat==164
gen smthhzdu=avghzdlatu*hzdminu


 
gen avghzdlatr=1/3*L1.hzdlatr+1/3*hzdlatr+1/3*F1.hzdlatr
replace avghzdlatr=3/5*hzdlatr+1/5*F1.hzdlatr+1/5*F2.hzdlatr if wagcat==1
replace avghzdlatr=1/5*L1.hzdlatr +2/5*hzdlatr+1/5*F1.hzdlatr + 1/5*F2.hzdlatr if wagcat==2
replace avghzdlatr=3/5*hzdlatr+1/5*L1.hzdlatr+1/5*L2.hzdlatr if wagcat==163
replace avghzdlatr=1/5*F1.hzdlatr +2/5*hzdlatr+1/5*L1.hzdlatr + 1/5*L2.hzdlatr if wagcat==162
replace avghzdlatr=1 if wagcat==164
gen smthhzdr=avghzdlatr*hzdminr
 
sum avghzdlatr hzdlatr 

sum if smthhzdr>1|smthhzdu>1
 
gen pu=1-exp(-smthhzdu)
gen pr=1-exp(-smthhzdr)
 
replace pu=1 if wagcat==164
replace pr=1 if wagcat==164
 
*Note pu and pr are segment hazards

*du and dr are segment densities, suru and surv are density survival functions for 
*the previous segment.
 
gen du=.
gen suru=.
gen dr=.
gen surr=.
 
 
replace du=pu if wagcat==1
replace suru=1-du if wagcat==1
replace dr=pr if wagcat==1
replace surr=1-dr if wagcat==1
 
local i=2
while `i'<=164 {
display `i'
gen probu=pu*L1.suru
gen probr=pr*L1.surr
gen survu=L1.suru-probu
gen survr=L1.surr-probr
replace du=probu if wagcat==`i'
replace dr=probr if wagcat==`i'
replace surr=survr if wagcat==`i'
replace suru=survu if wagcat==`i'
drop probu probr survu survr
local i=`i'+1
}
 
egen total=sum(fweight),by(prov year quarter agegrp edgrp mincat sub)
gen draw=fweight/total

egen weight=sum(fweight),by(x_sect)
 
sum du dr draw 

corr du draw

sort year prov quarter mincat wagcat sub
collapse du dr suru surr draw (rawsum) fweight [aw=weight],by(year prov quarter mincat wagcat sub)
 
if `j'==1 {
save $resultdir\males_fitted_unrestricted_sample,replace
} 

if `j'==2 {
save $resultdir\females_fitted_unrestricted_sample,replace
} 

if `j'==3 {
save $resultdir\males_fitted_restricted_sample, replace
} 

if `j'==4 {
save $resultdir\females_fitted_restricted_sample,replace
} 




}

log close
